import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:bot_toast/bot_toast.dart';

import '../../../data/services/api_service.dart';

class UpdatePwdController extends GetxController {
  // 服务实例
  final ApiService _apiService = Get.find<ApiService>();

  // 响应式变量
  final _isLoading = false.obs;
  final _isOldPasswordVisible = false.obs;
  final _isNewPasswordVisible = false.obs;
  final _isConfirmPasswordVisible = false.obs;
  final _canSubmit = false.obs;

  // 文本控制器
  final TextEditingController oldPasswordController = TextEditingController();
  final TextEditingController newPasswordController = TextEditingController();
  final TextEditingController confirmPasswordController =
      TextEditingController();

  // Getters
  bool get isLoading => _isLoading.value;
  bool get isOldPasswordVisible => _isOldPasswordVisible.value;
  bool get isNewPasswordVisible => _isNewPasswordVisible.value;
  bool get isConfirmPasswordVisible => _isConfirmPasswordVisible.value;
  bool get canSubmit => _canSubmit.value;

  @override
  void onInit() {
    super.onInit();
    // 添加输入监听
    oldPasswordController.addListener(_validateInputs);
    newPasswordController.addListener(_validateInputs);
    confirmPasswordController.addListener(_validateInputs);
  }

  @override
  void onClose() {
    oldPasswordController.removeListener(_validateInputs);
    newPasswordController.removeListener(_validateInputs);
    confirmPasswordController.removeListener(_validateInputs);
    oldPasswordController.dispose();
    newPasswordController.dispose();
    confirmPasswordController.dispose();
    super.onClose();
  }

  /// 验证输入并更新提交按钮状态
  void _validateInputs() {
    final oldPassword = oldPasswordController.text.trim();
    final newPassword = newPasswordController.text.trim();
    final confirmPassword = confirmPasswordController.text.trim();

    final isValid =
        oldPassword.isNotEmpty &&
        newPassword.isNotEmpty &&
        newPassword.length >= 6 &&
        confirmPassword.isNotEmpty &&
        newPassword == confirmPassword &&
        oldPassword != newPassword;

    _canSubmit.value = isValid;
  }

  /// 切换原密码可见性
  void toggleOldPasswordVisibility() {
    _isOldPasswordVisible.value = !_isOldPasswordVisible.value;
  }

  /// 切换新密码可见性
  void toggleNewPasswordVisibility() {
    _isNewPasswordVisible.value = !_isNewPasswordVisible.value;
  }

  /// 切换确认密码可见性
  void toggleConfirmPasswordVisibility() {
    _isConfirmPasswordVisible.value = !_isConfirmPasswordVisible.value;
  }

  /// 验证密码
  String? validateOldPassword(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入原密码';
    }
    return null;
  }

  /// 验证新密码
  String? validateNewPassword(String? value) {
    if (value == null || value.isEmpty) {
      return '请输入新密码';
    }
    if (value.length < 6) {
      return '密码长度不能少于6位';
    }
    return null;
  }

  /// 验证确认密码
  String? validateConfirmPassword(String? value) {
    if (value == null || value.isEmpty) {
      return '请确认新密码';
    }
    if (value != newPasswordController.text) {
      return '两次输入的密码不一致';
    }
    return null;
  }

  /// 修改密码
  Future<void> modifyPassword() async {
    // 验证输入
    final oldPassword = oldPasswordController.text.trim();
    final newPassword = newPasswordController.text.trim();
    final confirmPassword = confirmPasswordController.text.trim();

    if (oldPassword.isEmpty) {
      BotToast.showText(text: '请输入原密码');
      return;
    }

    if (newPassword.isEmpty) {
      BotToast.showText(text: '请输入新密码');
      return;
    }

    if (newPassword.length < 6) {
      BotToast.showText(text: '新密码长度不能少于6位');
      return;
    }

    if (confirmPassword.isEmpty) {
      BotToast.showText(text: '请确认新密码');
      return;
    }

    if (newPassword != confirmPassword) {
      BotToast.showText(text: '两次输入的密码不一致');
      return;
    }

    if (oldPassword == newPassword) {
      BotToast.showText(text: '新密码不能与原密码相同');
      return;
    }

    try {
      _isLoading.value = true;

      final result = await _apiService.modifyPassword(
        originPassword: oldPassword,
        newPassword: newPassword,
      );

      if (result != null) {
        BotToast.showText(text: '密码修改成功');
        Get.back();
      }
    } catch (e) {
      print('修改密码失败: $e');
      BotToast.showText(text: '网络环境不佳，请稍后尝试');
    } finally {
      _isLoading.value = false;
    }
  }
}
